//
//  AppDelegate.m
//  alipaydemo
//
//  Created by 李鹏飞 on 2020/4/23.
//  Copyright © 2020 李鹏飞. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import <AlipaySDK/AlipaySDK.h>
#import "WXApi.h"
#import "WXApiObject.h"
@interface AppDelegate ()

@end

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window.rootViewController = [ViewController new];
    [WXApi registerApp:@"wx15d9a60872e47468" universalLink:@"https://zxzlw.share2dlink.com/"];
    return YES;
}

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
    if ([url.host isEqualToString:@"pay"]) {
        //微信
        return [WXApi handleOpenURL:url delegate:self];
        
    } else if ([url.host isEqualToString:@"safepay"]){
        
        //支付宝
        [[AlipaySDK defaultService] processOrderWithPaymentResult:url standbyCallback:^(NSDictionary *resultDic) {
            NSInteger resultStatus = [[resultDic valueForKey:@"resultStatus"] integerValue];
            NSLog(@"支付宝回调resultDic:%@",resultDic);
            if (resultDic) {
                if (resultStatus == 9000) {
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"alipay_success" object:nil];
                   
                } else {
                    
                    [[NSNotificationCenter defaultCenter] postNotificationName:@"alipay_cancle" object:nil];
                    
                }
            } else {
                [[NSNotificationCenter defaultCenter] postNotificationName:@"alipay_faild" object:nil];
            }
        }];
    } 
    
    return YES;
}

#pragma mark 微信回调方法
-(void) onResp:(BaseResp*)resp
{
    //启动微信支付的response
    NSString *payResoult = [NSString stringWithFormat:@"errcode:%d", resp.errCode];
    if([resp isKindOfClass:[PayResp class]]){
        //支付返回结果，实际支付结果需要去微信服务器端查询
        switch (resp.errCode) {
            case 0:
                payResoult = @"支付结果：成功！";
                [[NSNotificationCenter defaultCenter] postNotificationName:@"paySuccess" object:nil];
                break;
            case -1:
                payResoult = @"支付结果：失败！";
                [[NSNotificationCenter defaultCenter] postNotificationName:@"payFail" object:nil];
                break;
            case -2:
                payResoult = @"用户已经退出支付！";
                [[NSNotificationCenter defaultCenter] postNotificationName:@"exit" object:nil];
                break;
            default:
                payResoult = [NSString stringWithFormat:@"支付结果：失败! retcode = %d, retstr = %@", resp.errCode,resp.errStr];
                break;
        }
    }
}
@end
